<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Learn_card extends User_Controller {

	public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }

	function index($card_num = 0, $title = 0, $activation_time = 0, $limit = 0){
		$this->load->model("Learn_cards");
		$post_data = $this->input->post();
		if(!empty($post_data['card_num'])){
			$card_num = $post_data['card_num'];
		}
		if(!empty($post_data['title'])){
			$title = $post_data['title'];
		}
		if(!empty($post_data['activation_time'])){
			$activation_time = $post_data['activation_time'];
		}
		$conditions['card_num'] = $card_num;
		$conditions['title'] = $title;
		$conditions['activation_time'] = $activation_time;
		$card_list = $this->Learn_cards->get_learn_card_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/my/learn_card/index/' . $card_num . '/' . $title . '/' . $activation_time;
		$page_config['total_rows'] = $card_list['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 7;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("menu", "learn_card");
		$this->smarty->assign("time", time());
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("card_list", $card_list['result']);
		$this->smarty->display("user/learn_card_list.html");
	}

	function useing(){
		if($_POST){
			$post_data = $this->input->post();
			if(empty($post_data['card_num'])){
				show_message("/my/learn_card/useing", "请填写学习卡卡号");exit;
			}
			if(empty($post_data['card_pwd'])){
				show_message("/my/learn_card/useing", "请填写学习卡密码");exit;
			}
			$card_info = get_field_by_where("o2o_school_learn_card", "*", "card_num='{$post_data['card_num']}'");
			if(!$card_info){
				show_message("/my/learn_card/useing", "学习卡不存在,请核对后再试！");exit;
			}
			if($card_info['is_activation'] != 0){
				show_message("/my/learn_card/useing", "该学习卡已激活,无法重复使用！");exit;
			}
			if($card_info['card_pwd'] != $post_data['card_pwd']){
				show_message("/my/learn_card/useing", "学习卡密码不正确,请核对后再试！");exit;
			}
			$this->load->model("Learn_cards");
			$item_list = $this->Learn_cards->get_learn_item($card_info['id']);
			if($item_list){
				$time = time();
				foreach ($item_list as $key => $value) {
					if($value['content_type'] == 1){
						$code_info = get_field_by_where("o2o_school_system_video_course_codes", "*", "course_id='{$value['content_id']}' AND status = 1 AND school_id = '{$this->school_id}'");
						if(!$code_info){
							show_message("/my/learn_card/useing", "该激活码包含的课程ID：".$value['content_id']."激活码不够，请联系网校");exit;
						}
						$sql = "UPDATE {$this->db->dbprefix}o2o_school_system_video_course_codes SET status = 2, use_uid='{$this->user_data['id']}', use_time='{$time}' WHERE id='{$code_info['id']}'";
			    		$this->db->query($sql);
					}elseif($value['content_type'] == 4){
						$code_info = get_field_by_where("o2o_school_system_topic_codes", "*", "subject_id='{$value['content_id']}' AND status = 1 AND school_id = '{$this->school_id}'");
						if(!$code_info){
							show_message("/my/learn_card/useing", "该激活码包含的题库ID：".$value['content_id']."激活码不够，请联系网校");exit;
						}
						$sql = "UPDATE {$this->db->dbprefix}o2o_school_system_topic_codes SET status = 2, use_uid='{$this->user_data['id']}', use_time='{$time}' WHERE id='{$code_info['id']}'";
			    		$this->db->query($sql);
					}
				}
			}else{
				show_message("/my/learn_card/useing", "学习卡是空的");exit;
			}
			$learn_card_data['user_id'] = $this->user_data['id'];
			$learn_card_data['is_activation'] = 1;
			$learn_card_data['activation_time'] = time();
			$this->db->where("card_num", $post_data['card_num']);
			$this->db->update("o2o_school_learn_card", $learn_card_data);
			if($this->db->affected_rows() > 0){
				$student_card_data['card_id'] = $card_info['id'];
				$student_card_data['user_id'] = $this->user_data['id'];
				$student_card_data['use_time'] = $learn_card_data['activation_time'];
				$student_card_data['effective_day'] = $card_info['expire_day'];
				$student_card_data['end_time'] = $learn_card_data['activation_time'] + $card_info['expire_day'] * 3600 * 24;
				$this->db->insert("o2o_school_student_learn_card", $student_card_data);

			    $this->db->where("id", $this->school_id);
			    $this->db->set("item_num", "item_num - 1", false);
			    $this->db->update("o2o_school");
			    
				show_message("/my/learn_card", "学习卡激活成功！");exit;
			}else{
				show_message("/my/learn_card/useing", "学习卡激活失败,请核对后再试！");exit;
			}
		}
		$this->smarty->assign("menu", "learn_card");
		$this->smarty->display("user/learn_card_use.html");
	}

	function show_item($card_id = 0){
		$this->load->model("Learn_cards");
		$item_list = $this->Learn_cards->get_learn_item($card_id);

		$tiku = $course = array();
		foreach ($item_list as $key => $value) {
			if($value['content_type'] == 1){
				$course[] = array("id"=>$value['content_id'],"title"=>$value['course_title']);
			}elseif($value['content_type'] == 4){
				$tiku[] = array("id"=>$value['content_id'],"title"=>$value['title']);
			}
		}
		$this->smarty->assign("tiku", $tiku);
		$this->smarty->assign("course", $course);
		$this->smarty->display("user/show_item.html");
	}
}
